var listColumns = [[
    {field: "id", checkbox: true},
    {field: "callerNumber", title: "报修人电话", align: "center", width: 100},
    {field: "source", title: "工单来源", align: "center", width: 80,formatter:function (value) {
        switch (value){
            case "1": return "电话报修";
            case "2": return "邮件报修";
            case "3": return "微信报修";
            default: return "";
        }
    }},
    {field: "problem", title: "报修问题", align: "center", width: 160},
    {field: "repairAddress", title: "报修地点", align: "center", width: 150},
    {field: "repairRemark", title: "报修备注", align: "center", width: 120},
    {field: "expectRepairTime", title: "期望上门时间", align: "center", width: 140},
    {field: "processSpecification", title: "处理说明", align: "center", width: 150},
    {field: "homeRepair", title: "是否上门", align: "center", width: 60,formatter:function (value) {
        switch (value){
            case false: return "否";
            case true: return "是";
            default: return "";
        }
    }},
    {field: "beginProcessTime", title: "开始处理时间", align: "center", width: 140},
    {field: "endProcessTime", title: "结束处理时间", align: "center", width: 140},
    {field: "reason", title: "原因", align: "center", width: 160},
    {field: "processRemark", title: "处理备注", align: "center", width: 120},
    {field: "status", title: "当前状态", align: "center", width: 80,formatter:function (value) {
        switch(value){
            case "0": return "创建";
            case "1": return "下发";
            case "2": return "维修中";
            case "3": return "回单";
            case "4": return "关闭";
            default: return "";
        }
    }},
    {field: "createTime", title: "创建时间", align: "center", width: 140},
    {field: "updateTime", title: "更新时间", align: "center", width: 140},
    {
        field: "handle", title: "处理", align: "center", width: 80, formatter: function (value, rowData) {
        var flag = rowData.status;
        var roleId = $("#session_roleId").val();
        if(roleId != 2 && roleId !=3 ){
            return "";
        }
        if (flag=='0') {
            return '<a class="layui-btn layui-btn-xs" onclick="handleRecord('+rowData.id+','+flag+')" style="text-decoration: none">下发任务</a>'
        }else if (flag=='1') {
            return '<a class="layui-btn layui-btn-xs" onclick="handleRecord('+rowData.id+','+flag+')" style="text-decoration: none">维修</a>'
        }else if (flag=='2') {
            return '<a class="layui-btn layui-btn-xs" onclick="handleRecord('+rowData.id+','+flag+')" style="text-decoration: none">回单</a>'
        }else if (flag=='3') {
            return '<a class="layui-btn layui-btn-xs" onclick="handleRecord('+rowData.id+','+flag+')" style="text-decoration: none">关闭</a>'
        }

    }
    }
]];

layui.config({
    base: "../static/agent-ui/js/"
});

$.fn.toJSON = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        var name = this.name;
        var value = this.value;
        var paths = this.name.split(".");
        var len = paths.length;
        var obj = o;
        $.each(paths, function (i, e) {
            if (i == len - 1) {
                if (obj[e]) {
                    if (!obj[e].push) {
                        obj[e] = [obj[e]];
                    }
                    obj[e].push(value || '');
                } else {
                    obj[e] = value || '';
                }
            } else {
                if (!obj[e]) {
                    obj[e] = {};
                }
            }
            obj = o[e];
        });
    });
    return o;
};

layui.use(["laypage", "layer", "validator", "tools", "form", "select", "laydate"], function () {
    var form = layui.form;
    var date = layui.laydate;
    date.render({
        elem: '#ringTime',
        type: 'datetime',
        range: true
    });


    var layer=layui.layer;

    $(function () {
        $('#list').datagrid({
            columns: listColumns,
            fitColumns: false,
            fit: true,
            singleSelect: true,
            pagination: true,
            pageSize: 30,
            pageList: [30, 50, 100, 1000],
            toolbar: '#receivedListHeader',
            method: 'get',
            url: ctx +'/workOrder/list',
            onLoadSuccess: function () {
                audiojs.events.ready(function () {
                    audiojs.createAll();
                });
            }
        });
    });

    $('#search_submit').on('click', function () {
        $('#list').datagrid('load', $('#search_form').toJSON());
    });

    $("#addData").on("click", function(){
        /* 再弹出添加界面 */
        layer.open({
            type:1,
            title:"添加工单",
            skin:"myclass",
            area:["50%"],
            content:$("#test").html(),
            success:function (layero,index) {
                form.render();
                layero.addClass('layui-form');
                layero.find('.layui-layer-btn0').attr('lay-filter', 'fromContent').attr('lay-submit', '');//将按钮弄成能提交的
                layui.each(layero.find("#expectRepairTime"),function (index,elem) {
                    date.render({
                        elem: elem,
                        type: 'datetime'
                    })
                })

            },
            btn:['提交并下单','关闭'],
            yes:function (index,layero) {
                form.on('submit(fromContent)', function (data) {
                    add(index,'1',layero)
                });
            },
            btn2:function (index,layero) {
                layer.close(index);
            }
        });

    });
});

function add(index,status,layero) {
    var callerNumber = $(layero).find('input[name=callerNumber]').val();
    var source = $(layero).find('select[name=source]').val();
    var problem = $(layero).find('input[name=problem]').val();
    var repairAddress = $(layero).find('input[name=repairAddress]').val();
    var repairRemark = $(layero).find('input[name=repairRemark]').val();
    var expectRepairTime = $(layero).find('input[name=expectRepairTime]').val();
    $.ajax({
        url:ctx+'/workOrder/add',
        type : 'POST',
        data:
            {
            callerNumber:callerNumber,
            problem:problem,
            source:source,
            status:status,
            repairAddress:repairAddress,
            repairRemark:repairRemark,
            expectRepairTimeStr:expectRepairTime
        },
        success:function () {
            layer.close(index);
            $('#list').datagrid('reload');
        }
    });
}
//  更新操作
function handleRecord(id,flag){
    if(flag =='0'){
        var json = {id:id,status:1};
        update(json);
    } else if(flag =='1'){// 售后接单的时候
        process_1(id,flag);
    }else if(flag =='2'){
        process_2(id,flag);
    }else if(flag=='3'){
        var json = {id:id,status:4};
        update(json,function () {
            $('#list').datagrid('reload')
        });
    }


}
// 售后接单的时候  弹出dialog  选择是否上门服务
// 如果不需要则直接填写相关说明，然后回单到话务员，如果需要，记录当前处理工单的开始时间
function process_1(id, flag) {
    layer.open({
        type:1,
        title:"维修",
        skin:"myclass",
        area:["500px","300px"],
        content:$("#weixiu").html(),
        success:function (layero,index) {
            var form = layui.form;
            form.render();
            layero.addClass('layui-form');
            layero.find('.layui-layer-btn0').attr('lay-filter', 'fromContent').attr('lay-submit', '');//将按钮弄成能提交的
        },
        btn:['提交','关闭'],
        yes:function (index,layero) {
            var form = layui.form;
            form.on('submit(fromContent)', function (data) {
                var homeRepair = $(layero).find('select[name=homeRepair]').val();
                var status;
                var json ;
                if(homeRepair=='0'){
                    status = '3';
                    var processSpecification = $(layero).find('input[name=processSpecification]').val();
                    json = {id:id,homeRepair:homeRepair,status:status,processSpecification:processSpecification};
                }else if(homeRepair=='1'){
                    status = '2';
                    json = {id:id,homeRepair:homeRepair,status:status};
                }
                update(json,function () {
                    layer.close(index);
                    $('#list').datagrid('reload');
                });
            });
        },
        btn2:function (index) {
            layer.close(index);
        }
    });
}

function process_2(id, flag) {
    layer.open({
        type:1,
        title:"处理事项",
        skin:"myclass",
        area:["500px","300px"],
        content:$("#huidan").html(),
        success:function (layero,index) {
            var form = layui.form;
            form.render();
            layero.addClass('layui-form');
            layero.find('.layui-layer-btn0').attr('lay-filter', 'fromContent').attr('lay-submit', '');//将按钮弄成能提交的
        },
        btn:['提交','关闭'],
        yes:function (index,layero) {
            var form = layui.form;
            form.on('submit(fromContent)', function (data) {
                var reason = $(layero).find('input[name=reason]').val();
                var processSpecification = $(layero).find('input[name=processSpecification]').val();
                var processRemark = $(layero).find('input[name=processRemark]').val();
                var json = {id:id,status:3,processSpecification:processSpecification,processRemark:processRemark,reason:reason};
                update(json,function () {
                    layer.close(index);
                    $('#list').datagrid('reload');
                });
            });
        },
        btn2:function (index) {
            layer.close(index);
        }
    });
}
function update(json,callback) {
    $.ajax({
        url:ctx+'/workOrder/update',
        type : 'POST',
        data:json,
        success:function () {
            if(callback !=null){
                callback();
            }

        }
    });
}




